# -*- coding:utf-8 -*-
from django.template import loader, Context
from django.http import HttpResponse, HttpResponseRedirect
from porabi.food.models import cafe, coment, critic, profile
#from porabi.food.viewsmethod import crcaptcha
from django.shortcuts import render_to_response
import datetime

############################################################################
#  indexfood(index page: porabi.ru/food/)                                  #
############################################################################
def indexfood(request):
   tcafe = cafe.objects.all()[0:39]
   t = loader.get_template("indexfood.html")
   #ca = crcaptcha()
   c = Context ({ 'cafes': tcafe,'user':request.user})
   return HttpResponse( t.render(c))


############################################################################
#  TOP & Shit pages                                                        #
############################################################################ 

def top30(request):
   t = loader.get_template("topshit30.html")
   tcafe = cafe.objects.order_by('-cafe_reiting')[0:29]
   c = Context ({ 'cafes':tcafe, 'user':request.user })
   return HttpResponse( t.render(c))

def shit(request):
   t = loader.get_template("topshit30.html")
   tcafe = cafe.objects.order_by('cafe_reiting')[0:29]
   c = Context ({ 'cafes':tcafe, 'user':request.user })
   return HttpResponse( t.render(c))

############################################################################
#  Page add Critic                                                         #
############################################################################ 

def criticadd(request):
  date = datetime.datetime.now()
  if request.user.is_authenticated():  
    if 'title' in request.POST:
        if(request.POST['title'] != "" or None):
            a = critic(
                    title = request.POST['title'],
                    text = request.POST['text'],
                    date = date,
                    user_id = request.user.id,
                    tags1 = request.POST['tag1']
                    #tag2 = request.POST['tag2']
                    #tag3 = request.POST['tag3']		
                    #tag4 = request.POST['tag4']
                    #tag5 = request.POST['tag5']
            ) 
            a.save()
        return HttpResponseRedirect("/food/")
    else:  
        t = loader.get_template("addcrit.html")
        c = Context({'user':request.user})
        return HttpResponse(t.render(c))
  else:
    t = loader.get_template(" erroradd.html")
    c = RequestContext({})
    return HttpResponse(t.render(c))

def profile(request):
    if request.user.is_authenticated():
        tprofile = request.user
        return render_to_response("profile.html", {'profile':tprofile, 'user':request.user})
    else:
        return HttpResponseRedirect('/food/')


############################################################################
#  indexfood(index page: porabi.ru/food/)                                  #
############################################################################
def index(request):
   t = loader.get_template("index.html")
   c = Context ({})
   return HttpResponse( t.render(c))

